# coding=utf8

import urllib
from app import create_app
from app.modules.apps.models import AppInfo
from app.modules.entrance.models import EntranceInfo
from app.modules.common.jsonFileUtil import saveJsonFile
from app.modules.switch.resources import getSwitchEnvParam
from config import BaseConfig

application = create_app(flask_config = 'local')


def main(_env_name, _zone_code, _entrance_types, _group, _file):
    with application.app_context():
        _resources = []
        if _entrance_types is not None:

            for _entrance_info in EntranceInfo.query.filter_by(env_name = _env_name, zone_code = _zone_code).filter(
                    EntranceInfo.type.in_(str(_entrance_types).split(','))).all():
                if 'jmx' == _entrance_info.type:
                    for _app in AppInfo.query.filter_by(entrance_id = _entrance_info.id, is_resource = '0').all():
                        _resources.append(
                            '%s://%s:%s' % (_entrance_info.prefix, _app.host, _app.jmx_port))
                elif 'elb' == _entrance_info.type:
                    _resources.append('%s://%s:%s@%s%s' % (
                        _entrance_info.prefix, str(_entrance_info.user).split(':')[0],
                        urllib.quote_plus(str(_entrance_info.user).split(':')[1]), _entrance_info.hosts,
                        _entrance_info.suffix))
                elif 'uschedule' == _entrance_info.type:
                    _resources.append(
                        '%s://%s%s' % (_entrance_info.prefix, _entrance_info.hosts, _entrance_info.suffix))
                elif 'haproxy' == _entrance_info.type:
                    _resources.append(
                        '%s://%s%s' % (_entrance_info.prefix, _entrance_info.hosts, _entrance_info.suffix))
                elif 'hosts' == _entrance_info.type:
                    _query = AppInfo.query.filter_by(entrance_id = _entrance_info.id, is_resource = '0')
                    if _zone_code in BaseConfig.HOSTS_GROUP_ZONE and _group is not None:
                        _query = _query.filter_by(group = _group)
                    for _app in _query.all():
                        _h_resource = '%s://%s:%s%s' % (
                            _entrance_info.prefix, _app.host, _app.agent_port, _entrance_info.suffix)
                        if _h_resource not in _resources:
                            _resources.append(_h_resource)

        _data = {"mode": "G%s" % _group, "tasks": _resources, "env": getSwitchEnvParam(_env_name, _zone_code)}
        print _data
        saveJsonFile(_file, _data)
        pass


if __name__ == '__main__':
    _env_name = 'staging'
    _zone_code = [
        {'code': 'uc.gzidc.area', 'name': '中国区'},
        # {'code': 'uc.usaws.area', 'name': '美国区'},
        # {'code': 'uc.euaws.area', 'name': '欧洲区'},
        {'code': 'uc.auaws.area', 'name': '澳洲区'}
    ]
    _entrance_types = 'jmx,hosts,uschedule,haproxy,elb'

    for _zone in _zone_code:
        main(_env_name, _zone['code'], _entrance_types, 'A',
             "/Users/wangjian/Desktop/switch/%s/switch_%s_A.json" % (_env_name, _zone['name']))
        if _zone['code'] in BaseConfig.HOSTS_GROUP_ZONE:
            main(_env_name, _zone['code'], _entrance_types, 'B',
                 "/Users/wangjian/Desktop/switch/%s/switch_%s_B.json" % (_env_name, _zone['name']))
